package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.BerthDTO;
import com.api.shopapi.entity.dto.BuildingDTO;
import com.api.shopapi.entity.dto.RoomDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * @author maochuan
 * 房间
 */
@RestController
@RequestMapping("Room")
public class RoomController extends BaseController {

    @Autowired
    @Qualifier("RoomServiceImplOne")
    private RoomService roomService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("BuildingServiceImplOne")
    private BuildingService buildingService;

    @Autowired
    @Qualifier("BerthServiceImplOne")
    private BerthService berthService;

    @Autowired
    @Qualifier("RoomTypeServiceImplOne")
    private RoomTypeService roomTypeService;


    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;


    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Insert(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String buildingCode = jsonObject.getString("buildingCode");
            int floor = jsonObject.getInteger("floor");
            String number = jsonObject.getString("number");
            String roomTypeCode = jsonObject.getString("roomTypeCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr,sign,staffCode,"/Room/Add");
            if(preCheck.getError() == 1){
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();
            //查询楼宇名称
            Building building =  buildingService.getByBuildingCode(buildingCode);
            if(building==null){
                return failed("楼宇不存在");
            }
            String buildingName = building.getName();
            Room room = roomService.getByNumber(number,buildingCode,institutionCode);
            if(room == null){
                room = new Room();
                room.setStaffCode(staffCode);
                room.setRoomCode(UUID.randomUUID().toString());
                room.setBuildingCode(buildingCode);
                room.setBuildingName(buildingName);
                room.setFloor(floor);
                room.setNumber(number);
                room.setRoomTypeCode(roomTypeCode);
                room.setStatus(0);
                room.setInstitutionCode(institutionCode);
                roomService.insert(room);
                Object data = new Object[]{};
                return success(data);
            }else {
                return failed("房间号已存在");
            }
        }catch (Exception e){
            insertError("/Room/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult EditByroomCode(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String buildingCode = jsonObject.getString("buildingCode");
            int floor = jsonObject.getInteger("floor");
            String number = jsonObject.getString("number");
            String roomTypeCode = jsonObject.getString("roomTypeCode");
            int status = jsonObject.getInteger("status");
            String roomCode = jsonObject.getString("roomCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr,sign,staffCode,"/Room/Edit");
            if(preCheck.getError() == 1){
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();
            Building building =  buildingService.getByBuildingCode(buildingCode);
            if(building==null){
                return failed("楼宇不存在");
            }
            String buildingName = building.getName();

            Room room = roomService.getByRoomCode(roomCode);

            Room room1 = roomService.getByNumber(number,buildingCode,institutionCode);
            if(room1!=null){
                if(!room.getRoomCode().equals(room1.getRoomCode())){
                    return failed("此房号已存在");
                }
                //判断状态如果为已住人，则不可改为禁用
                if(status==11){
                    if(room1.getStatus()==1){
                        return failed("此房间已有人入住，不可禁用");
                    }
                }
            }



            room.setBuildingCode(buildingCode);
            room.setBuildingName(buildingName);
            room.setFloor(floor);
            room.setNumber(number);
            room.setRoomTypeCode(roomTypeCode);
            room.setStatus(status);
            room.setInstitutionCode(institutionCode);
            roomService.updateByRoomCode(room);

            Object data = new Object[]{};
            return success(data);

        }catch (Exception e){
            insertError("/Room/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }



    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult GetListAll(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr,sign,staffCode,"/Room/List");
            if(preCheck.getError() == 1){
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();
            List<Room> listRoom = roomService.getListAllByInstitutionCode(institutionCode);
            return success(listRoom);
        }catch (Exception e){
            insertError("/Room/List", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    /**
     * 查询全部房间
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "ListRoom", method = RequestMethod.POST)
    public MyResult ListRoom(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/Room/ListRoom");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            //楼宇列表
            List<Building> listBuilding = buildingService.getListAllByInstitutionCode(institutionCode);
            //楼宇扩展列表
            List<BuildingDTO> listBuildingDTO = new ArrayList<>();

            for (Building building : listBuilding) {
                BuildingDTO buildingDTO = mapper.map(building, BuildingDTO.class);
                listBuildingDTO.add(buildingDTO);
            }


            for (BuildingDTO buildingDTO : listBuildingDTO) {
                //房间列表
                List<Room> listRoom = roomService.getListByBuildingCodeAndStatus0(buildingDTO.getBuildingCode());


                //房间扩展列表
                List<RoomDTO> listRoomDTO = new ArrayList<>();

                for (Room room : listRoom) {
                    RoomDTO roomDTO = mapper.map(room, RoomDTO.class);
                    listRoomDTO.add(roomDTO);
                }
                for(RoomDTO roomDTO:listRoomDTO){
                    String roomTypeCode1 = roomDTO.getRoomTypeCode();
                    RoomType roomType = roomTypeService.getByRoomTypeCode(roomTypeCode1);
                    roomDTO.setRoomTypeDTO(roomType);
                }
                buildingDTO.setListRoomDTO(listRoomDTO);


            }


            return success(listBuildingDTO);
        }catch (Exception e){
            insertError("/Room/ListRoom", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String buildingCode = jsonObject.getString("buildingCode");
            int number = jsonObject.getInteger("number");
            String roomTypeCode = jsonObject.getString("roomTypeCode");
            int status = jsonObject.getInteger("status");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr,sign,staffCode,"/Room/Page");
            if(preCheck.getError() == 1){
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            PageInfo<Room> pageInfoRoom = roomService.getPageList(pageIndex,pageSize,institutionCode,number,buildingCode,roomTypeCode,status);
            List<RoomDTO> roomDTOList = new ArrayList<>();
            for(Room room :pageInfoRoom.getList()){
                RoomDTO roomDTO = mapper.map(room,RoomDTO.class);
                roomDTOList.add(roomDTO);
            }
            for(RoomDTO roomDTO:roomDTOList){
                String roomTypeCode1 = roomDTO.getRoomTypeCode();
                RoomType roomType = roomTypeService.getByRoomTypeCode(roomTypeCode1);
                roomDTO.setRoomTypeDTO(roomType);
            }
            pageInfoRoom.setList(Collections.unmodifiableList(roomDTOList));
            return success(pageInfoRoom);
        }catch (Exception e){
            insertError("/Room/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "GovPage", method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String buildingCode = jsonObject.getString("buildingCode");
            int number = jsonObject.getInteger("number");
            String roomTypeCode = jsonObject.getString("roomTypeCode");
            int status = jsonObject.getInteger("status");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr,sign,staffCode,"/Room/Page");
            if(preCheck.getError() == 1){
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<Room> pageInfoRoom = roomService.pageByGovCode(pageIndex,pageSize,staff.getGovernmentCode());
            List<RoomDTO> roomDTOList = new ArrayList<>();
            for(Room room :pageInfoRoom.getList()){
                RoomDTO roomDTO = mapper.map(room,RoomDTO.class);
                roomDTOList.add(roomDTO);
            }
            for(RoomDTO roomDTO:roomDTOList){
                String roomTypeCode1 = roomDTO.getRoomTypeCode();
                RoomType roomType = roomTypeService.getByRoomTypeCode(roomTypeCode1);
                roomDTO.setRoomTypeDTO(roomType);
            }
            pageInfoRoom.setList(Collections.unmodifiableList(roomDTOList));

            return success(pageInfoRoom);
        }catch (Exception e){
            insertError("/Room/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

}
